package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.CmsSubject;
import com.itheima.mapper.CmsSubjectMapper;
import com.itheima.service.db.CmsSubjectService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class CmsSubjectServiceImpl implements CmsSubjectService {
    @Autowired
    CmsSubjectMapper cmsSubjectMapper;

    @Override
    public List<CmsSubject> findAllSubject() {
        LambdaQueryWrapper<CmsSubject>lqw=new LambdaQueryWrapper<>();
        lqw.orderByDesc(CmsSubject::getId);
        List<CmsSubject> cmsSubjects = cmsSubjectMapper.selectList(lqw);
        return cmsSubjects;

    }

    @Override
    public PageBeanVo findSubjectList(String keyword, Integer pageNum, Integer pageSize) {
        //分页构造器
        Page<CmsSubject> page = new Page<>(pageNum,pageSize);

        //条件构造器
        LambdaQueryWrapper<CmsSubject> qw = new LambdaQueryWrapper();
        qw.like(StrUtil.isNotEmpty(keyword),CmsSubject::getTitle,keyword)
                .orderByAsc(CmsSubject::getCreateTime);

        Page<CmsSubject> cmsSubjectPage = cmsSubjectMapper.selectPage(page, qw);
        return new PageBeanVo(pageNum,pageSize, cmsSubjectPage.getTotal(),cmsSubjectPage.getRecords());
    }
}
